package cn.study.zgm.nettyall.channel;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * packageName cn.study.zgm.nettyall.channel
 *
 * @author 你的名字
 * @version JDK 8
 * @className TestWalkFile (此处以class为例)
 * @date 2025/2/24
 * @description TODO
 */
public class TestFileWalkFile {
    public static void main(String[] args) throws IOException {
//        m1();
//        m2();
        m3();
    }

    private static void m3() throws IOException {
        Path sourcePath = Paths.get("E:\\temp\\data");
        Path targetPath = Paths.get("E:\\temp\\datacopy");

        Files.walk(sourcePath,FileVisitOption.FOLLOW_LINKS).forEach(item->{
            try {
                String targetName = item.toString().replace(sourcePath.toString(), targetPath.toString());
                if (Files.isDirectory(item)){
                    Files.createDirectory(Paths.get(targetName));
                }else if (Files.isRegularFile(item)){
                    Files.copy(item,Paths.get(targetName));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        });

    }

    // 删除指定文件夹
    private static void m2() throws IOException {
        Files.walkFileTree(Paths.get("E:\\temp\\data - 副本"),new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println(">>>>>>>>>>>>>>>>>开始："+dir);
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file);
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("<<<<<<<<<<<<<<<<开始："+dir);
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });

    }

    // 统计一个文件夹中包含的目录、文件、exe文件个数
    private static void m1() throws IOException {
        // 统计Ollama下文件夹和文件个数
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
        AtomicInteger exeFileCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("E:\\software\\Ollama"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("==========:"+dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir,attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("==========:"+file);
                fileCount.incrementAndGet();
                if (file.toString().endsWith(".exe")){
                    exeFileCount.incrementAndGet();
                }
                return super.visitFile(file,attrs);
            }
        });
        System.out.println("文件目录个数："+dirCount);
        System.out.println("====文件个数："+fileCount);
        System.out.println("=exe文件个数："+exeFileCount);
    }
}
